package com.frxuexi.park2305.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.UserBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.room.UserEntity
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.lib_base.utils.ArouterService
import com.frxuexi.park2305.R
import com.frxuexi.park2305.databinding.ActivityLoginBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding,UserIntent,UserViewModel>() {
    override val binding: ActivityLoginBinding by lazy { ActivityLoginBinding.inflate(layoutInflater) }
    override val vm: UserViewModel by viewModels<UserViewModel>()

    override fun initData() {
        val role = SPUtils.getInstance().getInt(Const.ROLE, -1)
        if(role!=-1){
            start(role)
        }


        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        Log.i("lr",it.data.toString())
                        ToastUtils.showLong("登录成功")
                        val userBean = it.data as UserBean
                        SPUtils.getInstance().apply {
                            put(Const.TOKEN,userBean.token)
                            put(Const.UNAME,userBean.username)
                            put(Const.UID,userBean.id)
                            put(Const.ROLE,userBean.roleid)
                        }

                        start(userBean.roleid)

                        /*val s = it.data as String
                        val pass =s.split("-")[0]
                        if(pass==binding.loginPass.text.toString()){
                            ToastUtils.showLong("登录成功")
                            start(20)
                        }else{
                            ToastUtils.showLong("密码不对")
                        }*/
                    }
                    is UiState.onError->{
//                        ToastUtils.showLong("账号不存在")
                    }

                    else -> {}
                }
            }
        }
    }

    fun start(rid:Int){
        ArouterManager.nav(this,"/app/MainActivity")
        finish()
        /*if(rid==20){
//            ARouter.getInstance().build("/app/MainActivity").navigation(this,ArouterService())
            ArouterManager.nav(this,"/app/MainActivity")
            finish()
        }else{
            ArouterManager.nav(this,"/app/WebActivity")
//            ARouter.getInstance().build("/app/WebActivity").navigation()
            finish()
        }*/
    }

    override fun initView() {

        binding.btnLogin.setOnClickListener {
            val uname = binding.loginUname.text.toString()
            val pass = binding.loginPass.text.toString()
            if(uname.isNullOrEmpty() || pass.isNullOrEmpty()){
                ToastUtils.showLong("用户名和密码不能为空")
                return@setOnClickListener
            }

            if(!uname.matches(Regex("[A-z0-9_]{2,11}"))){
                ToastUtils.showLong("不是合法用户名")
                return@setOnClickListener
            }

            vm.sendIntent(UserIntent.login(uname, pass))
//            vm.sendIntent(UserIntent.login(uname))
        }

        binding.loginRegister.setOnClickListener {
            ArouterManager.nav(this,"/app/RegisterActivity")
        }
    }

}